import datetime

from constant import COMMENT_RESULTS
from spider import utils, config
import pandas

from spider.ytb_v3_api.ytb_api import YoutubeAPI
from x_archives.settings import BASE_DIR


def comment_etl(comments, limit_count=0):
    validated_data = []
    for i in range(len(comments['items'])):
        # 进行点赞数过滤
        if limit_count > 0:
            if comments['items'][i]['snippet']['topLevelComment']['snippet']['likeCount'] < limit_count:
                continue
        # 数据清洗
        # todo:翻译、态度分析
        validated_data.append({
            'authorDisplayName': comments['items'][i]['snippet']['topLevelComment']['snippet']['authorDisplayName'],
            'likeCount': comments['items'][i]['snippet']['topLevelComment']['snippet']['likeCount'],
            'textOriginal': comments['items'][i]['snippet']['topLevelComment']['snippet']['textOriginal'],
            # 'textOriginal': utils.improve_format(comments['items'][i]['snippet']['topLevelComment']['snippet']['textOriginal']),
        })
    return validated_data


# 添加翻译文本
def add_zh(data: list):
    for i in data:
        q = i['textOriginal']
        tran_str = utils.baidufanyi(q)
        i['zh'] = tran_str


# 保存评论为csv
def save_to_csv(data: list, filename: str = 'comment', path=COMMENT_RESULTS):
    df = pandas.DataFrame(data)
    now = datetime.datetime.now().strftime("%Y%m%d%H%M")
    filename = path + '/' + filename + now + '.csv'
    # utf_8_sig防止excel打开出现乱码
    df.to_csv(filename, index=False, encoding='utf_8')


def csv_to_data(filename):
    df = pandas.read_csv(filename)
    data_list = df.to_dict(orient='records')
    return data_list

if __name__ == '__main__':
    api = YoutubeAPI(config.GOOGLE_APIKEY)
    comments = api.get_comment_threads_by_video_id('lRtedsK_6fs', order='relevance')
    comments = comment_etl(comments, 5)
    add_zh(comments)
    save_to_csv(comments)
